package com.maiyuren.flutter_text_reader;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import io.flutter.Log;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.StandardMessageCodec;
import io.flutter.plugin.platform.PlatformView;
import io.flutter.plugin.platform.PlatformViewFactory;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

/// 原生的组件
public class TextReaderView extends LinearLayout {
    com.bifan.txtreaderlib.main.TxtReaderView _view = null;
    Integer _id = 0;

    public TextReaderView(@NonNull Context context, Integer id) {
        super(context);
        _id = id;
        this.setLayoutParams(new ViewGroup.LayoutParams(-1, -1));
        _view = new com.bifan.txtreaderlib.main.TxtReaderView();
        _view.setLayoutParams(new ViewGroup.LayoutParams(-1, -1));

        this.addView(_view);
    }

    public TextReaderView(@NonNull Context context, AttributeSet as) {
        super(context, as);
        this.setLayoutParams(new ViewGroup.LayoutParams(-1, -1));
        _view.setLayoutParams(new ViewGroup.LayoutParams(-1, -1));
        this.addView(_view);
    }

    void load(String path, boolean isHor) {
        com.bifan.txtreaderlib.main.TxtConfig.saveIsOnVerticalPageMode(this, !isHor);
        _view.loadTxtFile(path, new com.bifan.txtreaderlib.main.ILoadListener() {
            @Override
            public void onSuccess() {
                //加载成功回调
            }

            @Override
            public void onFail(com.bifan.txtreaderlib.main.TxtMsg txtMsg) {
                //加载失败回调
            }

            @Override
            public void onMessage(String message) {
                //加载过程信息回调
            }
        });
    }
}

/// 一个继承PlatformView类 返回具体的原生组件
class TextReaderPlatformView implements PlatformView {
    Context context;
    Integer _id = 0;

    public TextReaderView view = null;

    public static Map<Integer, TextReaderPlatformView> s_views = new HashMap<Integer, TextReaderPlatformView>();

    TextReaderPlatformView(Context context, int viewId) {
        this.context = context;
        this._id = viewId;
        view = new TextReaderView(context, _id);
        s_views.put(_id, this);
    }

    /// 这里返回原生组件
    @Nullable
    @Override
    public View getView() {
        return view;
    }

    @Override
    public void dispose() {
        s_views.put(_id, null);
    }
}

/// 新建一个工厂类 返回 PlatformView 类实例
class TextReaderPlatformViewFactory extends PlatformViewFactory {

    /// 与flutter中 creationParams creationParamsCodec 对应 不实现此方法args则为null
    public TextReaderPlatformViewFactory() {
        super(StandardMessageCodec.INSTANCE);
    }

    @NonNull
    @Override
    public PlatformView create(Context context, int viewId, @Nullable Object args) {
        //args 根据这个参数可以返回不同的view
        return new TextReaderPlatformView(context, viewId);
    }
}
